package com.boss.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boss.entity.User;
import com.boss.service.UserService;
import com.boss.util.ResultVOUtil;
import com.boss.vo.ResultVO;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/list")
    public ResultVO getList() {
        return ResultVOUtil.success(userService.list());
    }

    @PostMapping("/login")
    public ResultVO login(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = userService.getOne(queryWrapper);
        String md5Pass = DigestUtils.md5DigestAsHex(password.getBytes());
        ResultVO resultVO = new ResultVO();
        if (user == null) {
            resultVO.setCode(-1);
            resultVO.setMsg("用户不存在");
        } else {
            if (!user.getPassword().equals(md5Pass)) {
                resultVO.setCode(-2);
                resultVO.setMsg("密码错误");
            } else {
                resultVO.setCode(0);
                resultVO.setMsg("登录成功");
                resultVO.setData(user);
            }
        }
        return resultVO;
    }

    @PostMapping("/register")
    public ResultVO register(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User hasUser = userService.getOne(queryWrapper);
        ResultVO resultVO = new ResultVO();
        if (hasUser!=null) {
            resultVO.setCode(-1);
            resultVO.setMsg("用户已存在");
        }else{
            User user = new User();
            String md5Pass = DigestUtils.md5DigestAsHex(password.getBytes());
            user.setUsername(username);
            user.setPassword(md5Pass);
            user.setSex("男");
            user.setAge("18");
            user.setEdu("本科");
            user.setSchool("泉州师范学院");
            user.setCover("https://img.bosszhipin.com/boss/avatar/avatar_16.png");
            userService.save(user);

            resultVO.setCode(0);
            resultVO.setMsg("注册成功");
            resultVO.setData(user);
        }

        return resultVO;
    }

    @GetMapping("/detail")
    public ResultVO detail(String id) {
        return ResultVOUtil.success(userService.getById(id));
    }
    @PostMapping("/update")
    public ResultVO update(String id,String sex,String cover,String age,String edu,String school){
        User user = new User();
        user.setId(id);
        user.setSex(sex);
        user.setCover(cover);
        user.setAge(age);
        user.setSchool(school);
        user.setEdu(edu);
        return ResultVOUtil.success(userService.updateById(user));
    }
    @GetMapping("/userlist")
    public ResultVO Userlist(String id){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.notLike("_id",id);
        return ResultVOUtil.success(userService.list(queryWrapper));
    }
}


